<template>
    <el-card>
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm"
            :size="formSize" status-icon>

            <el-form-item label="姓名" prop="RYD_Name">
                <el-input v-model="ruleForm.RYD_Name" />
            </el-form-item>
            <el-form-item label="联系方式" prop="RYD_Phone">
                <el-input v-model="ruleForm.RYD_Phone" />
            </el-form-item>
            <el-form-item label="IT注册号" prop="RYD_RegId">
                <el-input v-model="ruleForm.RYD_RegId" />
            </el-form-item>
            <el-form-item label="负责类别" prop="RYD_DemandTypeNo">
                <el-checkbox-group v-model="ruleForm.RYD_DemandTypeNo" :min="1">
                    <el-checkbox v-for="item in typeList" :key="item.ryD_DemandTypeNo" :label="item.ryD_DemandTypeNo">
                        {{ item.ryD_DemandTypeName }}
                    </el-checkbox>
                </el-checkbox-group>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>

        </el-form>
    </el-card>
</template>
  
<script setup>
import { inject, reactive, ref } from 'vue'
// eslint-disable-next-line no-unused-vars
import { Delete, Download, Plus, ZoomIn } from '@element-plus/icons-vue'

import axios from '@/utils/axios'
// eslint-disable-next-line no-unused-vars
import { ElMessageBox, ElNotification } from 'element-plus';
import router from '@/router';


const formSize = ref('default')
const ruleFormRef = ref()
const ruleForm = ref({
    RYD_Name: '',
    RYD_Phone: '',
    RYD_RegId: '',
    RYD_DemandTypeNo: [],
})



let RYD_UserId = inject('RYD_UserId');
let RYD_Company = ref();
// const disabled = ref(false)

let typeList = ref();


ElMessageBox.confirm(
    '该功能只允许IT部门使用，是否继续？',
    '提示',
    {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }
).then(() => {

}).catch(()=>{
    router.back();
})

const rules = reactive({
    RYD_Name: [
        { required: true, message: '请输入姓名', trigger: 'blur' },
    ],
    RYD_Phone: [
        { required: true, message: '请输入分机号或者手机号', trigger: 'blur' },
    ],
    RYD_RegId: [
        { required: true, message: '请输入IT注册号', trigger: 'blur' },
    ],
    RYD_DemandTypeNo: [
        { required: true, message: '请选择你负责的事件类别', trigger: 'blur' }
    ]
})
// let srcList = ref([]);
const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid) => {
        if (valid) {
            addDemand();
        }
    })
}

const addDemand = () => {
    axios({
        method: 'put',
        url: '/api/UserInfo',
        data: {
            ...ruleForm.value,
            RYD_UserId: RYD_UserId,
            RYD_Company: RYD_Company.value
        }
    }).then(res => {

        if (res != "") {

            ElNotification({
                title: '提示',
                message: '注册成功',
                type: 'success',
            })
            router.push({
                name: 'RefView', query: {
                    name: 'WindowsDemandListDef',
                    isHide: true
                }
            })
        } else {

            ElNotification({
                title: '提示',
                message: '失败',
                type: 'error',
            })
        }

    }).catch(() => {

        ElNotification({
            title: '提示',
            message: '失败',
            type: 'error',
        })
    })
}

const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}




initData();
function initData() {

    Promise.all(
        [


            axios({
                method: 'get',
                url: '/api/GetCompany?RYD_UserId=' + RYD_UserId
            }).then(res => {
                RYD_Company.value = res;


                axios({
                    method: 'get',
                    url: '/api/DemandType?RYD_Company=' + res
                }).then(typeListres => {
                    typeList.value = typeListres;
                })
            })
        ]
    ).then(() => {
        // console.log("🚀 ~ file: WindowsAddDemand.vue ~ line 189 ~ initData ~ res", res)
        // loading.value = false;
    })
}

</script>
<style>
.DemandFileImage {
    width: 100%;
}

.removeSpan {
    position: absolute;
    right: 0;
    top: 0;
    background-color: rgb(143, 143, 143);
    height: 20px;
    width: 20px;
    text-align: center;
    line-height: 20px;
    color: #fff;
    border-radius: 0 5px 0 5px;
    cursor: pointer;
}
</style>